//
// Created by denglibin on 2021/2/4.
// 渲染图片

#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#define true 1
#define false 0
static const int WIN_WIDTH = 1366;
static const int WIN_HEIGHT = 768;
static SDL_Window *win; //窗口指针
static SDL_Surface  *winSurface;
static SDL_Renderer *winRenderer;
static int quit = 0;
static SDL_Texture *pngTexture;

/**
 * 加载图片 返回纹理
 * @param file
 * @return 纹理
 */
static SDL_Texture* loadImage(const char * file){
    SDL_Texture * texture = IMG_LoadTexture(winRenderer, file);
    if(texture == NULL){
        printf("IMG_LoadTexture ERROR:%s\n", SDL_GetError());
        exit(1);
    }
    return texture;
}

/**
 * 创建并显示窗口
 */
static void createAndShowWin(){
    win = SDL_CreateWindow("demo2", SDL_WINDOWPOS_UNDEFINED,
                           SDL_WINDOWPOS_UNDEFINED,
                           WIN_WIDTH, WIN_HEIGHT,
                           SDL_WINDOW_SHOWN);
    if(win == NULL){
        printf("SDL_CreateWindow ERROR:%s\n", SDL_GetError());
        exit(1);
    }
    //获取窗口显示层
    winSurface = SDL_GetWindowSurface(win);

    //主窗口渲染器
    winRenderer = SDL_CreateRenderer(
            win,
            -1,  //序号 -1表示第一个
            SDL_RENDERER_ACCELERATED//使用硬件加速
            | SDL_RENDERER_PRESENTVSYNC); //和刷新频率同步
}

/**
 * 根据图片初始化纹理
 */
static void initTexture(){
    pngTexture = loadImage("img/favorite.png");
}
/**
 * 渲染图片
 */
static void showImage(const char *file, int  x, int y){
    SDL_Texture* texture =  loadImage(file); //纹理
    SDL_Rect  pos; //位置
    pos.x = x;
    pos.y = y;
    //获取纹理宽高
    SDL_QueryTexture(texture, NULL, NULL, &pos.w, &pos.h);
    //将纹理复制到渲染器上
    SDL_RenderCopy(winRenderer, //渲染器
                   texture,
                   NULL,
                   &pos);
    SDL_RenderPresent(winRenderer); //展示
}

static void showTexture(SDL_Texture * texture, int x, int y){
    SDL_Rect  pos; //位置
    pos.x = x;
    pos.y = y;
    //获取纹理宽高
    SDL_QueryTexture(texture, NULL, NULL, &pos.w, &pos.h);
    //将纹理复制到渲染器上
    SDL_RenderCopy(winRenderer, //渲染器
                   texture,
                   NULL,
                   &pos);
    SDL_RenderPresent(winRenderer); //展示
}
/**
 * 关闭资源
 */
static void close(){
    // 清理资源
    SDL_DestroyTexture(pngTexture);
    SDL_FreeSurface(winSurface);
    SDL_DestroyRenderer(winRenderer);
    SDL_DestroyWindow(win);
    SDL_Quit();
}
/**
 * 事件处理
 * @param e
 */
static void keyDownEvent(SDL_Event* e){
    switch (e->key.keysym.sym) {
        case SDLK_ESCAPE:
            quit = true;
            break;
        case SDLK_DOWN:
            break;
        case SDLK_UP:
            break;
        default:
            break;
    }

}

/**
 * 演示1 渲染图片
 */
static void demo1ShowPng(){
    for(int i = 0; i < 5;i++){
       showTexture(pngTexture, i * 200, 0);
    }
}
static void demo2MovePng(){
    int alpha = 0;
    int flag = 1;
    SDL_SetTextureBlendMode(pngTexture, SDL_BLENDMODE_BLEND);
    for(int i = 0; i < 1000;i++){
        SDL_RenderClear(winRenderer); //清除
        SDL_SetTextureAlphaMod(pngTexture, alpha); //修改alpha值，实现明暗效果
        if(alpha >= 255 && flag){
            flag = 0;
        }
        if(alpha <= 0 && !flag){
            flag = 1;
        }
        if(flag){
            alpha++;
        }else {
            alpha --;
        }

        showTexture(pngTexture, i * 1, 0); //重新渲染
        SDL_Delay(10);
    }

}

/**
 * 初始化
 */
static void init(){
   createAndShowWin();
   initTexture();

}
/**
 * 演示
 */
static void demo(){
    //demo1ShowPng();//演示1
   // demo2MovePng();//演示2
    SDL_Thread *thread = SDL_CreateThread((SDL_ThreadFunction) demo2MovePng, "demo", NULL); //在单独的线程中执行
    if ( thread == NULL ) {
        fprintf(stderr, "无法创建线程： %s/n", SDL_GetError());
        return;
    }
   // SDL_WaitThread(thread, NULL);

}

/**
 * 事件循环
 */
static void eventLoop(){
    SDL_Event  event;
    while (!quit)
    {//主消息循环
        SDL_WaitEvent(&event);

        switch (event.type)
        {
            //用户从菜单要求退出程序
            case SDL_QUIT:
                quit = 1;
                break;
            case SDL_KEYDOWN:
                keyDownEvent(&event);
                break;
            default:
                break;
        }
    }
}

int main2(int argc, char* argv[]){

    init();
    demo();
    eventLoop();
    close();
    return 0;
}